package cn.lili.controller.system.message;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.message.entity.dos.MemberMessage;
import cn.lili.modules.message.entity.vos.MemberMessageQueryVO;
import cn.lili.modules.message.service.MemberMessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 买家端,会员站内消息接口
 *
 * @author Bulbasaur
 * @since 2020/11/16 10:07 下午
 */
@RestController
@Api(tags = "会员站内消息接口")
@RequestMapping("/system/message/user")
@RequiredArgsConstructor
public class UserMessageController {

    /**
     * 会员站内消息
     */
    private final MemberMessageService memberMessageService;

    @ApiOperation(value = "未读消息数量")
    @GetMapping("/unReadCount")
    public ResultMessage<Long> page() {
        return ResultUtil.data(memberMessageService.unReadCount());
    }

    @ApiOperation(value = "分页获取用户站内消息")
    @GetMapping
    public ResultMessage<Page<MemberMessage>> page(MemberMessageQueryVO memberMessageQueryVO, PageVO page) {
        memberMessageQueryVO.setMemberId(UserContext.getCurrentUser().getExtendId());
        return ResultUtil.data(memberMessageService.getPage(memberMessageQueryVO, page));
    }

    @ApiOperation(value = "消息已读")
    @ApiImplicitParam(name = "messageId", value = "会员消息id", required = true, paramType = "path")
    @PutMapping("/{message_id}")
    public ResultMessage<Boolean> read(@PathVariable("message_id") String messageId) {
        memberMessageService.read(messageId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "消息放入回收站")
    @ApiImplicitParam(name = "messageId", value = "会员消息id", required = true, paramType = "path")
    @DeleteMapping("/{message_id}")
    public ResultMessage<Boolean> deleteMessage(@PathVariable("message_id") String messageId) {
        return ResultUtil.data(memberMessageService.deleteMessage(messageId));

    }


    @ApiOperation(value = "全部消息已读")
    @PutMapping("/readAll")
    public ResultMessage<Boolean> readAll() {
        memberMessageService.readAll();
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除全部消息")
    @DeleteMapping("/deleteAll")
    public ResultMessage<Boolean> deleteAll() {
        memberMessageService.deleteAll();
        return ResultUtil.success();
    }

}
